package net.gui;

import net.pages.GererFamillesMedicaments;
import net.pages.GererFichesVisites;
import net.pages.GererFraisForfaits;
import net.pages.GererMedicaments;
import net.pages.GererPraticiens;
import net.pages.GererRapportsVisites;
import net.pages.GererStatuts;
import net.pages.GererStatutsFiches;
import net.pages.GererTypesPraticiens;
import net.pages.GererVisiteurs;
import net.pages.PageAccueil;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wb.swt.SWTResourceManager;

public class Accueil {

	protected Shell shlGsbadminAccueil;
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	public Composite composite;
	private Composite activeComposite;

	public void setActiveComposite(Composite composite) {
		if (this.activeComposite != null) {
			this.activeComposite.dispose();
		}
		this.activeComposite = composite;
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Accueil window = new Accueil();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		PageAccueil pageAccueil = new PageAccueil(composite, SWT.NONE);
		pageAccueil.setBackground(SWTResourceManager.getColor(247, 244, 227));
		pageAccueil.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
		setActiveComposite(pageAccueil);
		shlGsbadminAccueil.open();
		shlGsbadminAccueil.layout();
		while (!shlGsbadminAccueil.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	protected void createContents() {

		shlGsbadminAccueil = new Shell(Display.getDefault(), SWT.RESIZE
				| SWT.NO_SCROLL | SWT.CLOSE);

		shlGsbadminAccueil.setSize(1079, 607);
		shlGsbadminAccueil.setImage(SWTResourceManager.getImage(Accueil.class, "/images/favicon.ico"));
		shlGsbadminAccueil.setText("GSB_ADMIN - Login");
		shlGsbadminAccueil.setLayout(new FormLayout());

		Composite menu = new Composite(shlGsbadminAccueil, SWT.NONE);
		FormData fd_menu = new FormData();
		fd_menu.bottom = new FormAttachment(0, 554);
		fd_menu.right = new FormAttachment(0, 240);
		fd_menu.top = new FormAttachment(0, 118);
		fd_menu.left = new FormAttachment(0);
		menu.setLayoutData(fd_menu);
		menu.setBackgroundImage(SWTResourceManager.getImage(Accueil.class, "/images/menu-bg.jpg"));

		Label lblMenuTitre = new Label(menu, SWT.HORIZONTAL | SWT.CENTER);
		lblMenuTitre.setForeground(SWTResourceManager.getColor(160, 82, 45));
		lblMenuTitre.setFont(SWTResourceManager.getFont("Segoe UI", 16, SWT.BOLD));
		lblMenuTitre.setBounds(26, 21, 177, 39);
		lblMenuTitre.setBackground(SWTResourceManager.getColor(247, 244, 227));
		lblMenuTitre.setText("---- Menu ----");

		ExpandBar expandBar = new ExpandBar(menu, SWT.NONE);
		expandBar.setBounds(10, 66, 220, 356);
		expandBar.setBackground(SWTResourceManager.getColor(247, 244, 227));

		ExpandItem xpndtmGestionVisiteurs = new ExpandItem(expandBar, SWT.NONE);
		xpndtmGestionVisiteurs.setExpanded(true);
		xpndtmGestionVisiteurs.setText("Gestion Visiteurs");

		Composite MenuVisiteurs = new Composite(expandBar, SWT.NONE);
		xpndtmGestionVisiteurs.setControl(MenuVisiteurs);
		MenuVisiteurs.setLayout(new FormLayout());
		MenuVisiteurs.setBackground(SWTResourceManager.getColor(247, 244, 227));

		final Link menu_gerervisiteurs = new Link(MenuVisiteurs, SWT.NONE);
		menu_gerervisiteurs.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererVisiteurs.class)) {
					GererVisiteurs GererVisiteur = new GererVisiteurs(composite, SWT.NONE);
					GererVisiteur.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererVisiteur);
				}
				//
			}
		});
		FormData fd_menu_gerervisiteurs = new FormData();
		fd_menu_gerervisiteurs.right = new FormAttachment(100, -115);
		fd_menu_gerervisiteurs.left = new FormAttachment(0, 20);
		menu_gerervisiteurs.setLayoutData(fd_menu_gerervisiteurs);
		menu_gerervisiteurs.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gerervisiteurs.setText("<a>G\u00E9rer Visiteurs</a>");
		

		Link menu_gererstatuts = new Link(MenuVisiteurs, SWT.NONE);
		menu_gererstatuts.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererStatuts.class)) {
					GererStatuts GererStatuts = new GererStatuts(composite, SWT.NONE);
					GererStatuts.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererStatuts);
				}
				//
			}
		});
		fd_menu_gerervisiteurs.bottom = new FormAttachment(100, -24);
		FormData fd_menu_gererstatuts = new FormData();
		fd_menu_gererstatuts.bottom = new FormAttachment(100, -3);
		fd_menu_gererstatuts.right = new FormAttachment(100, -103);
		fd_menu_gererstatuts.left = new FormAttachment(0, 20);
		menu_gererstatuts.setLayoutData(fd_menu_gererstatuts);
		menu_gererstatuts.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gererstatuts.setText("<a>G\u00E9rer les Statuts</a>");
		xpndtmGestionVisiteurs.setHeight(50);

		ExpandItem xpndtmGestionFrais = new ExpandItem(expandBar, SWT.NONE);
		xpndtmGestionFrais.setExpanded(true);
		xpndtmGestionFrais.setText("Gestion Frais");

		Composite MenuFrais = new Composite(expandBar, SWT.NONE);
		xpndtmGestionFrais.setControl(MenuFrais);
		MenuFrais.setBackground(SWTResourceManager.getColor(247, 244, 227));
		xpndtmGestionFrais.setHeight(75);

		Link menu_gererfraisforfaits = new Link(MenuFrais, SWT.NONE);
		menu_gererfraisforfaits.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererFraisForfaits.class)) {
					GererFraisForfaits GererFraisForfaits = new GererFraisForfaits(composite, SWT.NONE);
					GererFraisForfaits.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererFraisForfaits);
				}
				//
			}
		});
		menu_gererfraisforfaits.setBounds(22, 6, 110, 15);
		menu_gererfraisforfaits.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gererfraisforfaits.setText("<a>G\u00E9rer les frais forfaits</a>");

		Link menu_gererstatutsfiches = new Link(MenuFrais, SWT.NONE);
		menu_gererstatutsfiches.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererStatutsFiches.class)) {
					GererStatutsFiches GererStatutsFiches = new GererStatutsFiches(composite, SWT.NONE);
					GererStatutsFiches.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererStatutsFiches);
				}
				//
			}
		});
		menu_gererstatutsfiches.setBounds(22, 27, 126, 15);
		menu_gererstatutsfiches.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gererstatutsfiches.setText("<a>G\u00E9rer les Statuts Fiches</a>");

		Link menu_gererfichesvisites = new Link(MenuFrais, SWT.NONE);
		menu_gererfichesvisites.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererFichesVisites.class)) {
					GererFichesVisites GererFichesVisites = new GererFichesVisites(composite, SWT.NONE);
					GererFichesVisites.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererFichesVisites);
				}
				//
			}
		});
		menu_gererfichesvisites.setBounds(22, 48, 133, 15);
		menu_gererfichesvisites.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gererfichesvisites.setText("<a>G\u00E9rer les Fiches Visites</a>");

		ExpandItem xpndtmGestionCompteRendus = new ExpandItem(expandBar, SWT.NONE);
		xpndtmGestionCompteRendus.setExpanded(true);
		xpndtmGestionCompteRendus.setText("Gestion Compte Rendus");

		Composite MenuGestion = new Composite(expandBar, SWT.NONE);
		xpndtmGestionCompteRendus.setControl(MenuGestion);
		xpndtmGestionCompteRendus.setHeight(120);
		MenuGestion.setBackground(SWTResourceManager.getColor(247, 244, 227));
		MenuGestion.setLayout(null);

		Link menu_gererpraticiens = new Link(MenuGestion, SWT.NONE);
		menu_gererpraticiens.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererPraticiens.class)) {
					GererPraticiens GererPraticiens = new GererPraticiens(composite, SWT.NONE);
					GererPraticiens.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererPraticiens);
				}
				//
			}
		});
		menu_gererpraticiens.setBounds(24, 10, 97, 15);
		menu_gererpraticiens.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gererpraticiens.setText("<a>G\u00E9rer Practiciens</a>");

		Link menu_gerertypespraticiens = new Link(MenuGestion, SWT.NONE);
		menu_gerertypespraticiens.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererTypesPraticiens.class)) {
					GererTypesPraticiens GererTypesPraticiens = new GererTypesPraticiens(composite, SWT.NONE);
					GererTypesPraticiens.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererTypesPraticiens);
				}
				//
			}
		});
		menu_gerertypespraticiens.setBounds(24, 31, 152, 15);
		menu_gerertypespraticiens.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gerertypespraticiens.setText("<a>G\u00E9rer les types de Practiciens</a>");

		Link menu_gerermedicaments = new Link(MenuGestion, SWT.NONE);
		menu_gerermedicaments.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererMedicaments.class)) {
					GererMedicaments GererMedicaments = new GererMedicaments(composite, SWT.NONE);
					GererMedicaments.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererMedicaments);
				}
				//
			}
		});
		menu_gerermedicaments.setBounds(24, 52, 120, 15);
		menu_gerermedicaments.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gerermedicaments.setText("<a>G\u00E9rer les M\u00E9dicaments</a>");

		Link menu_gererfamillesmedicaments = new Link(MenuGestion, SWT.NONE);
		menu_gererfamillesmedicaments.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererFamillesMedicaments.class)) {
					GererFamillesMedicaments GererFamillesMedicaments = new GererFamillesMedicaments(composite, SWT.NONE);
					GererFamillesMedicaments.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererFamillesMedicaments);
				}
				//
			}
		});
		menu_gererfamillesmedicaments.setBounds(24, 73, 180, 15);
		menu_gererfamillesmedicaments.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gererfamillesmedicaments.setText("<a>G\u00E9rer les familles de M\u00E9dicaments</a>");

		Link menu_gererrapportsvisites = new Link(MenuGestion, SWT.NONE);
		menu_gererrapportsvisites.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (activeComposite == null
						|| !activeComposite.getClass().equals(GererRapportsVisites.class)) {
					GererRapportsVisites GererRapportsVisites = new GererRapportsVisites(composite, SWT.NONE);
					GererRapportsVisites.setBounds(0, 0, composite.getSize().x, composite.getSize().y);
					setActiveComposite(GererRapportsVisites);
				}
				//
			}
		});
		menu_gererrapportsvisites.setBounds(24, 94, 144, 15);
		menu_gererrapportsvisites.setBackground(SWTResourceManager.getColor(247, 244, 227));
		menu_gererrapportsvisites.setText("<a>G\u00E9rer les rapports de Visites</a>");

		Composite logo = new Composite(shlGsbadminAccueil, SWT.NONE);
		FormData fd_logo = new FormData();
		fd_logo.bottom = new FormAttachment(0, 125);
		fd_logo.right = new FormAttachment(0, 240);
		fd_logo.top = new FormAttachment(0);
		fd_logo.left = new FormAttachment(0);
		logo.setLayoutData(fd_logo);
		logo.setBackgroundImage(SWTResourceManager.getImage(Accueil.class, "/images/left-top.jpg"));

		final Composite fond = new Composite(shlGsbadminAccueil, SWT.NONE);
		FormData fd_fond = new FormData();
		fd_fond.bottom = new FormAttachment(0, 554);
		fd_fond.top = new FormAttachment(0, 151);
		fd_fond.left = new FormAttachment(0);
		fond.setLayoutData(fd_fond);
		fond.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		fond.setBackground(SWTResourceManager.getColor(247, 244, 227));
		fond.setLayout(new FormLayout());

		final Composite BarreVerticale = new Composite(fond, SWT.NONE);
		FormData fd_BarreVerticale = new FormData();
		fd_BarreVerticale.bottom = new FormAttachment(0, 34);
		fd_BarreVerticale.right = new FormAttachment(0, 1008);
		fd_BarreVerticale.top = new FormAttachment(0);
		fd_BarreVerticale.left = new FormAttachment(0, 239);
		BarreVerticale.setLayoutData(fd_BarreVerticale);
		BarreVerticale.setBackground(SWTResourceManager.getColor(247, 244, 227));

		Label lblNewLabel = new Label(BarreVerticale, SWT.NONE);
		lblNewLabel.setAlignment(SWT.RIGHT);
		lblNewLabel.setBounds(230, 10, 360, 15);
		lblNewLabel.setText("Bonjour "+Utilisateur.getPrenom()+" " + Utilisateur.getNom());
		lblNewLabel.setBackground(SWTResourceManager.getColor(247, 244, 227));

		Link link_deco = new Link(BarreVerticale, SWT.NONE);
		link_deco.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Login pagelogin = new Login();
				shlGsbadminAccueil.close();
				pagelogin.open();
			}
		});
		link_deco.setBounds(596, 10, 83, 15);
		link_deco.setBackground(SWTResourceManager.getColor(247, 244, 227));
		link_deco.setText("<a>(D\u00E9connexion)</a>");

		composite = new Composite(fond, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_composite = new FormData();
		fd_composite.top = new FormAttachment(BarreVerticale, 6);
		fd_composite.left = new FormAttachment(BarreVerticale, 10, SWT.LEFT);
		fd_composite.bottom = new FormAttachment(0, 373);
		fd_composite.right = new FormAttachment(0, 998);
		composite.setLayoutData(fd_composite);
		formToolkit.adapt(composite);
		formToolkit.paintBordersFor(composite);

		Composite bleu = new Composite(shlGsbadminAccueil, SWT.NONE);
		FormData fd_bleu = new FormData();
		fd_bleu.bottom = new FormAttachment(0, 157);
		fd_bleu.right = new FormAttachment(0, 185);
		fd_bleu.top = new FormAttachment(0, 118);
		fd_bleu.left = new FormAttachment(0);
		bleu.setLayoutData(fd_bleu);
		bleu.setBackgroundImage(SWTResourceManager.getImage(Accueil.class, "/images/bleu.jpg"));

		final Composite header = new Composite(shlGsbadminAccueil, SWT.NONE);
		FormData fd_header = new FormData();
		fd_header.bottom = new FormAttachment(0, 157);
		fd_header.right = new FormAttachment(0, 1008);
		fd_header.top = new FormAttachment(0);
		fd_header.left = new FormAttachment(0, 177);
		header.setLayoutData(fd_header);
		header.setBackground(SWTResourceManager.getColor(255, 255, 240));
		header.setBackgroundImage(SWTResourceManager.getImage(Accueil.class, "/images/header.jpg"));
		header.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		final Composite header_blue = new Composite(shlGsbadminAccueil, SWT.NONE);	
		FormData fd_header_blue_1 = new FormData();
		fd_header_blue_1.bottom = new FormAttachment(0, 160);
		fd_header_blue_1.top = new FormAttachment(0, 9);
		fd_header_blue_1.left = new FormAttachment(header);
		header_blue.setLayoutData(fd_header_blue_1);
		header_blue.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		header_blue.setBackgroundImage(SWTResourceManager.getImage(Accueil.class, "/images/arrow3.gif"));
		formToolkit.adapt(header_blue);
		formToolkit.paintBordersFor(header_blue);
		
		
		shlGsbadminAccueil.addListener(SWT.Resize, new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
						
				FormData fd_header_blue = new FormData();
				fd_header_blue.bottom = new FormAttachment(0, 160);
				fd_header_blue.top = new FormAttachment(0, 9);
				fd_header_blue.left = new FormAttachment(header);
				fd_header_blue.right = new FormAttachment(0, shlGsbadminAccueil.getBounds().width);
				header_blue.setLayoutData(fd_header_blue);
				
				FormData fd_fond = new FormData();
				fd_fond.bottom = new FormAttachment(0, shlGsbadminAccueil.getBounds().height);
				fd_fond.top = new FormAttachment(0, 151);
				fd_fond.left = new FormAttachment(0);
				fd_fond.right = new FormAttachment(0, shlGsbadminAccueil.getBounds().width);
				fond.setLayoutData(fd_fond);
				
				FormData fd_composite = new FormData();
				fd_composite.top = new FormAttachment(BarreVerticale, 6);
				fd_composite.left = new FormAttachment(BarreVerticale, 10, SWT.LEFT);
				fd_composite.bottom = new FormAttachment(0, shlGsbadminAccueil.getBounds().height-200);
				fd_composite.right = new FormAttachment(0, shlGsbadminAccueil.getBounds().width-50);
				composite.setLayoutData(fd_composite);			
				formToolkit.adapt(composite);
				formToolkit.paintBordersFor(composite);
			}
		});
		//Fenetre de demande de confirmation de fermeture de l'application
		//Ajout d'un Listener sur la fen�tre principale
		shlGsbadminAccueil.addShellListener(new org.eclipse.swt.events.ShellAdapter()
	     {
            public void shellClosed(org.eclipse.swt.events.ShellEvent e)
            {
                // On demande confirmation � l'utilisateur
                MessageBox mb = new MessageBox(shlGsbadminAccueil, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
               //Titre de la fen�tre
                mb.setText("Demande de confirmation");
                //Message de la MessageBox
                mb.setMessage("Voulez-vous vraiment quitter?");
                e.doit = mb.open() == SWT.OK;
            }
        });
		}
	}

